﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mail;
using System.Data;
using System.Data.SqlClient;
using System.Data.Odbc;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace EmailMarketingCampaign
{
    public partial class sendEmail : System.Web.UI.Page
    {
        DataSource ds = new DataSource();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private bool SendMail(string subject, string body, string fromMail, string toMail)
        {
            try
            {

                MailMessage myMail = new MailMessage();
                myMail.Subject = subject;
                myMail.Body = body;
                myMail.From = fromMail;
                myMail.To = toMail;
                
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "luna2005@126.com"); 
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "z1x2c3"); 

                SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
                SmtpMail.Send(myMail);

                return true;
            }
            catch
            {
                return false;
            }
        }

        protected void SendImmediately_Click(object sender, EventArgs e)
        {
            OdbcDataReader dataReader = ds.ExecuteReader("select subject, body,from_name, from_email, to_email from mails where id='14'");
            string subject = "";
            string body = "";
            string fromName = "";
            string fromEmail = "";
            string toEmail = "";

            while (dataReader.Read())
            {
                subject = dataReader.GetString(0);
                body = dataReader.GetString(1);
                fromName = dataReader.GetString(2);
                fromEmail = dataReader.GetString(3);
                toEmail = dataReader.GetString(4);
            }
            dataReader.Close();
            ds.CloseConnection();
              
            bool flag = SendMail(subject, body, fromEmail, toEmail);
            if (flag == true)
            {
                Response.Write("<script>alert('Send Successfully!');</script>");
            }
            else
            {
                Response.Write("<script>alert('Failed to send!');</script>");
            }
        }

        protected void btnSendByScheduled_Click(object sender, EventArgs e)
        {
            string date= txtDate.Text;
            string hh = ddlHour.SelectedItem.Value;
            string mm = ddlMinutes.SelectedItem.Value;
            string type = ddlType.SelectedItem.Value;
            //Label1.Text = date + " " + hh + ":" + mm + " " + type;
            DateTime dt = Convert.ToDateTime(Label1.Text);
            //Response.Write(dt);

            string requestQuery = "Insert into mail_schedules (mail_id,member_id,send_at,status) " +
                                                "Values (?,?,?,?)";
            OdbcCommand reqComm = new OdbcCommand();
            OdbcParameter pMAid = new OdbcParameter("@mailID", OdbcType.Int);
            OdbcParameter pMEid = new OdbcParameter("@memberID", OdbcType.Int);
            OdbcParameter pSend = new OdbcParameter("@sendAT", OdbcType.DateTime);
            OdbcParameter pStatus = new OdbcParameter("@status", OdbcType.Char, 1);
            OdbcParameter[] pc = new OdbcParameter[] { pMAid, pMEid, pSend, pStatus};
            pMAid.Value = 14;
            pMEid.Value = 1;
            pSend.Value = dt;
            pStatus.Value = "N";
            reqComm.Parameters.AddRange(pc);
            reqComm.CommandText = requestQuery;
            ds.ExecuteInsert(reqComm);
        }

    }
}
